import 'package:fakegpt/bean/MappingBean.dart';
import 'package:fakegpt/page/chat/logic.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class ChatPage extends GetView<ChatLogic> {
  const ChatPage({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(ChatLogic());
    final state = controller.state;
    return Scaffold(
      appBar: AppBar(
        title: const Text("Fake ChatGpt"),
      ),
      body: SafeArea(
          child: Column(
        children: [
          Expanded(
            child: GetBuilder<ChatLogic>(
              id: "list",
              assignId: true,
              builder: (logic) {
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: ListView.builder(
                      itemCount: state.list.length,
                      itemBuilder: (c, i) => getItem(state.list[i])),
                );
              },
            ),
          ),
          Container(
            padding: const EdgeInsets.all(10),
            decoration: BoxDecoration(color: Colors.white, boxShadow: [
              BoxShadow(
                  color: Colors.grey.shade300, spreadRadius: 5, blurRadius: 5)
            ]),
            child: Row(
              children: [
                Expanded(
                    child: TextField(
                  controller: state.textController,
                  decoration: const InputDecoration(hintText: "请输入内容"),
                )),
                ElevatedButton(
                    onPressed: () {
                      controller.send();
                    },
                    child: const Icon(Icons.send))
              ],
            ),
          )
        ],
      )),
    );
  }

  Widget getItem(MappingBean bean) {
    if (bean.message?.content?.parts?.first == null ||
        bean.message?.content?.parts?.first.isEmpty == true ||
        bean.message?.content?.parts?.first == "null") {
      return Container();
    }
    if (bean.message?.author?.role == 'user') {
      return Padding(
        padding: const EdgeInsets.symmetric(vertical: 5),
        child: Row(
          children: [
            const SizedBox(
              width: 30,
            ),
            Expanded(
              child: Align(
                alignment: Alignment.centerRight,
                child: Container(
                    padding: const EdgeInsets.all(5),
                    decoration: const BoxDecoration(
                        color: Colors.blue,
                        borderRadius: BorderRadius.all(Radius.circular(10))),
                    child: Text(
                      "${bean.message?.content?.parts?.first}",
                      textAlign: TextAlign.end,
                      style: const TextStyle(color: Colors.white),
                    )),
              ),
            ),
          ],
        ),
      );
    } else {
      return Padding(
        padding: const EdgeInsets.symmetric(vertical: 5),
        child: Row(
          children: [
            Expanded(
              child: Align(
                alignment: Alignment.centerLeft,
                child: Container(
                    padding: const EdgeInsets.all(5),
                    decoration: BoxDecoration(
                        color: Colors.green.shade400,
                        borderRadius:
                            const BorderRadius.all(Radius.circular(10))),
                    child: Text(
                      textAlign: TextAlign.start,
                      "${bean.message?.content?.parts?.first}",
                      style: const TextStyle(color: Colors.white),
                    )),
              ),
            ),
            const SizedBox(
              width: 30,
            )
          ],
        ),
      );
    }
  }
}
